/**
 * Created by Administrator on 2017/4/18 0018.
 */
import React from "react";
import Select from "../../../common/Select"
import Relay from "react-relay";
import FindUsersByEnterpriseIdAndPidMutation from "../mutation/FindUsersByEnterpriseIdAndPidMutation";
import ReactKey from "../../../util/ReactKey";
import ReactDOM from 'react-dom';
import {message} from 'antd';
import {Button} from 'fly';

export default class PeopleDetail extends React.Component{
    constructor(props){
        super(props);
        this.state={
            isedit:false,
            linkman:[{text:'请选择联系人',value:'0'}],
            partBEnName:'',
            partBUserName:'',
            partBUserPhone:'',
            istexteditshow:true,
            defaultseletValue:''
        };
        this.linkman = [{text:'请选择联系人',value:'0'}];
        this.deobj = this.props.context['deobj']||{};
    }

    componentWillMount(){
        // console.log(this.props.context);
        if(!this.props.context['isedit']){
            this.setState({
                isedit: this.props.context['isedit'],
                partBEnName:this.props.context['enName'],
                partBUserName:this.props.context['name'],
                partBUserPhone:this.props.context['phone'],
            });
        }else{
            this.setState({
                isedit: true,
                partBEnName:this.props.context['enName'],
                partBUserName:this.props.context['name'],
                partBUserPhone:this.props.context['phone'],
            });
        }
        // console.warn(this.props.index+'---'+this.props.context['isedit']);
    }

    _ChooseItem(id){
        if(id ==0){
            this.linkman = [{text:'请选择联系人',value:'0'}];
            this.setState({
                linkman:this.linkman
            });
            return;
        }
        this.linkman = [{text:'请选择联系人',value:'0'}];
        Relay.Store.commitUpdate(
            new FindUsersByEnterpriseIdAndPidMutation({
                enterpriseId : id,
                /*销售订单管理权限的用户*/
                pid:'11'
            }),
            {
                onFailure: () => {

                },
                onSuccess: (response) => {

                    let errorCode = response.findUsersByEnterpriseIdAndPid.findUsersByEnterpriseIdAndPid.errorCode;
                    if(errorCode=="0"){
                        let userModelList = response.findUsersByEnterpriseIdAndPid.findUsersByEnterpriseIdAndPid.userModelList;
                        userModelList.map((context,index)=>{
                            this.linkman.push({text:context.name+" "+context.phone,value:context.uid})
                        });
                        this.setState({
                            linkman:this.linkman
                        },()=>{
                            this.refs.linkman.setState({
                                value:'0',
                                defaultValue:'0'
                            });
                        });
                    }
                }
            }
        )
    }
    _SaveOrder(){
        let partbname = ReactDOM.findDOMNode(this.refs.partbname).selectedOptions[0].innerHTML;
        let partbusetinfo = ReactDOM.findDOMNode(this.refs.linkman).selectedOptions[0].innerHTML;
        let username = '';
        let userphone = '';
        message.config({
            top: 30,
        })
        if(partbusetinfo!='请选择联系人'){
            username = partbusetinfo.split(' ')[0];
            userphone = partbusetinfo.split(' ')[1];
        }
        if(partbname=='请选择供应方'||partbname==''){
            message.warning('供应方企业必须填写');
            return;
        }
        if(partbusetinfo=='请选择联系人'||partbusetinfo==''){
            message.warning('联系人必须填写');
            return;
        }
        this.props.itemOverEdit();
        this.setState({
            partBEnName:partbname,
            partBUserName:username,
            partBUserPhone:userphone,
            isedit:true
        });
        this.props.context['isedit'] = true;

        /*选择的企业从列表去除*/
        if(this.refs.partbname.state.value==''){
            this.deobj = {'text':partbname,'value':this.refs.partbname.state.defaultValue};
        }else{
            this.deobj = {'text':partbname,'value':this.refs.partbname.state.value};
        }
        this.props.context['deobj'] = this.deobj
        let tempindex = -1;
        this.props.enterpriseList.map((temp, index)=> {
            if ( JSON.stringify(this.deobj) == JSON.stringify(temp)) {
                tempindex = index;
            }
        });
        if (tempindex != -1) {
            this.props.enterpriseList.splice(tempindex, 1);
        }
        this.props.context['enId'] = this.refs.partbname.state.value||this.refs.partbname.state.defaultValue;
        this.props.context['toUserId'] = this.refs.linkman.state.value;
        this.props.context['enName'] = partbname;
        this.props.context['name'] = username;
        this.props.context['phone'] = userphone;
    }
    _CancelOrder(){

    }

    _EditPatb(){
        this.props.itemIsEdit();
        this.props.enterpriseList .splice(1,0,this.deobj);
        this.setState({
            isedit:false,
            defaultseletValue:this.deobj.value
        },()=>{
            this._ChooseItem(this.deobj.value);
        });

    }

    _AddPartb(){
        this.props.addPartbItem();
    }

    _DelteItem(index){
        this.props.delteItem(index);
    }

//display:block;white-space:nowrap; overflow:hidden; text-overflow:ellipsis;

    render(){
        return (
            <div >
                {
                    this.state.isedit?
                        <div className="order_creatOrder_publicOrder_contextpartb_borderdiv">
                            <div style={{marginTop:30,marginLeft:20,display:'block',whiteSpace:'nowrap',overflow:'hidden',textOverflow:'ellipsis',width:400}}>供应方 ：{this.state.partBEnName}</div>
                            <div style={{marginTop:20,marginLeft:20}}>联系人 ：{this.state.partBUserName}</div>
                            <div style={{marginTop:20,marginLeft:20}}>联系电话 ：{this.state.partBUserPhone}</div>
                            <div className="order_creatOrder_publicOrder_contextpartbedit_editdiv" style={this.state.istexteditshow?{}:{display:'none'}}
                                 onClick={this._EditPatb.bind(this)}>编辑</div>
                            <div className="order_creatOrder_publicOrder_contextpartbedit_addpartbdiv" style={(this.props.totallength-1==this.props.index&&this.state.istexteditshow)?{}:{display:'none'}}
                                 onClick={this._AddPartb.bind(this)}>添加供应方</div>

                        </div>
                        :

                        <div className="order_creatOrder_publicOrder_contextpartbedit_borderdiv">
                            <p className="order_creatOrder_publicOrder_contextpartbedit_borderpb">
                                <span className="order_creatOrder_publicOrder_contextpartbedit_borderspanb">供应方：</span>
                                <Select oArr={this.props.enterpriseList}  style={{marginTop:"20px",width:290,height:32,display:"block",borderColor:"#36b3b3",marginLeft:10,borderRadius:"5px"}}
                                        onselectchose={this._ChooseItem.bind(this)} ref={"partbname"}
                                        defaultValue={this.state.defaultseletValue}/>
                            </p>
                            <p  className="order_creatOrder_publicOrder_contextpartbedit_borderpb">
                                <span className="order_creatOrder_publicOrder_contextpartbedit_borderspanb">联系人：</span>
                                <Select oArr={this.state.linkman}  style={{width:290,height:32,display:"block",border:"1px lolid #36b3b3",borderColor:"#36b3b3",marginLeft:10,borderRadius:"5px"}}
                                        ref={'linkman'} defaultValue={''} key={ReactKey.key}/>

                            </p>
                            <div style={{marginLeft:'78px',marginTop:'8px'}}>
                                <Button onClick={this._SaveOrder.bind(this)}>保存</Button>
                                <div className="order_creatOrder_publicOrder_contextpartbedit_deletdiv" style={this.props.totallength==1?{display:'none'}:{}}
                                     onClick={this._DelteItem.bind(this,this.props.index)}>删除本条</div>
                            </div>
                            <button className="order_creatOrder_publicOrder_contextpartbedit_canceldiv"
                                    style={{display:'none'}}
                                    onClick={this._CancelOrder.bind(this)}>取消</button>


                        </div>
                }

            </div>
        )
    }
}
